import { BettingType } from '@/enums'
import { Match, Market, Selection, BetslipItemStatus, BettingBonusType } from '@/types'
import { getItemById } from './lib/array'
import { floor } from 'lodash-es'

export const getBettingCode = (type: BettingType) => {
	switch (type) {
		case BettingType.handicap:
			return 'HC'
		case BettingType.ou:
			return 'OU'
		case BettingType.moneyline:
			return 'ML'
		// case BettingType.exactScore:
		// 	return 'QA'
	}
}

export const isBettingLock = (match: Match, market: Market, selection: Selection) =>
	match.isDisabled || market.isDisabled || selection.isDisabled

export const getMarket = (match: Match, marketId: string) =>
	getItemById<Market>(match?.markets, marketId, 'marketId')

export const getSelection = (match: Match, marketId: string, selectionId: string) => {
	const market = getMarket(match, marketId)
	const selection = getItemById<Selection>(market?.selections, selectionId)
	return selection
}

export const isTransition = (status: BetslipItemStatus | undefined) =>
	status !== null &&
	(BetslipItemStatus.confirming === status ||
		BetslipItemStatus.success === status ||
		BetslipItemStatus.fail === status)

export const getComboOddsBaisc = (list: number[]) =>
	floor(
		list.reduce((prev, item) => item * prev, 1),
		2
	)

export const getComboOddsBonus = (
	list: number[],
	comboBonus: BettingBonusType[],
	comboBonusMinTrueOdds: number
) => {
	const comboHasLowOdds = list.some((item) => item < comboBonusMinTrueOdds)
	if (!comboHasLowOdds) {
		for (let index = comboBonus.length - 1; index >= 0; index--) {
			const { number, percent } = comboBonus[index]
			if (list.length >= number) {
				return parseFloat(percent) / 100
			}
		}
	}
	return 0
}

export const getComboOdds = (
	list: number[],
	comboBonus: BettingBonusType[],
	comboBonusMinTrueOdds: number
) => {
	return getComboOddsBaisc(list) * (1 + getComboOddsBonus(list, comboBonus, comboBonusMinTrueOdds))
}
